<template>
  <div>
    <div class="card" style="margin-bottom: 5px">
      <el-input style="width: 240px; margin: 10px; margin-right: 5px" prefix-icon="Search" placeholder="请输入标题查询"
                v-model="data.title"></el-input>
      <el-button type="primary" @click="load">查询</el-button>
      <el-button type="info" @click="reset">重置</el-button>
    </div>
    <div class="card" style="margin-bottom: 5px">
      <el-button type="primary" @click="handleAdd">新增</el-button>
      <el-button type="danger" @click="delBatch">批量删除</el-button>
    </div>
    <div class="card" style="margin-bottom: 5px">
      <el-table :data="data.tableData" stripe @selection-change="handleSelectionChange">
        <el-table-column type="selection"  width="55" />
        <el-table-column label="标题" prop="title"/>
        <el-table-column label="封面">
          <template #default="scope">
            <el-image v-if="scope.row.img" :src="scope.row.img" :preview-src-list=[scope.row.img] preview-teleported="true" style="display:block;width: 100px ;height: 100px;border-radius: 10px"/>
          </template>
        </el-table-column>
        <el-table-column label="简介" prop="description" show-overflow-tooltip="true"/>
<!--        查看富文本-->
        <el-table-column label="内容">
        <template #default="scope">
          <el-button type="primary" @click="view(scope.row.content)">查看内容</el-button>
        </template>
        </el-table-column>
        <el-table-column label="发布时间" prop="time"/>
        <el-table-column label="操作" width="120px">
          <template #default="scope">
            <el-button  type="primary" :icon="Edit" circle="true" @click="handleUpdate (scope.row)"></el-button>
            <el-button  type="danger" :icon="Delete" circle="true" @click="del(scope.row.id)"></el-button>
          </template>
        </el-table-column>
      </el-table>
      <div style="margin-top: 10px">
        <el-pagination
            v-model:current-page="data.pageNum"
            v-model:page-size="data.pageSize"
            :page-sizes="[5, 10, 15, 20]"
            :size="size"
            background
            layout="total, sizes, prev, pager, next, jumper"
            :total="data.total"
            @size-change="load"
            @current-change="load"
        />
      </div>
    </div>

    <el-dialog v-model="data.formVisible" title="文章信息" width="50%" destroy-on-close="true">
      <el-form ref="formRef"  :model="data.form" label-width="80px" style="padding-right: 50px;padding-top: 20px">
        <el-form-item label="标题" prop="title" >
          <el-input  v-model="data.form.title" autocomplete="off" placeholder="输入标题" />
        </el-form-item>
        <el-form-item label="封面"  >
          <el-upload
              action="http://localhost:9090/files/upload"
              list-type ="picture"
              :on-success="handleImgSuccess"
          >
            <el-button type="primary">上传封面</el-button>
          </el-upload>
        </el-form-item>
        <el-form-item label="简介" prop="description" >
          <el-input type="textarea" rows="3" v-model="data.form.description" autocomplete="off" placeholder="输入简介" />
        </el-form-item>
        <el-form-item label="内容"  >
<!--          富文本，要加import-->
          <div style="border: 1px solid #ccc; width: 100%">
            <Toolbar
                style="border-bottom: 1px solid #ccc"
                :editor="editorRef"
                :mode="mode"
            />
            <Editor
                style="height: 500px; overflow-y: hidden;"
                v-model="data.form.content"
                :mode="mode"
                :defaultConfig="editorConfig"
                @onCreated="handleCreated"
            />
          </div>
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button @click="data.formVisible = false">取 消</el-button>
          <el-button type="primary" @click="save">保存</el-button>
        </div>
      </template>
    </el-dialog>

    <el-dialog title="内容" v-model="data.viewVisible" width="50%" :close-on-click-modal="false" destroy-on-close>
      <div class="editor-content-view" style="padding: 20px" v-html="data.content"></div>
      <template #footer>
    <span class="dialog-footer">
      <el-button @click="data.viewVisible = false">关 闭</el-button>
    </span>
      </template>
    </el-dialog>

  </div>
</template>

<script setup>

import {reactive, ref} from "vue";
import {Delete, Edit, Search} from "@element-plus/icons-vue";
//import  request from "axios";
import axios  from "axios";
import request from "@/utils/request.js"
import {ElMessage, ElMessageBox} from "element-plus";
import '@wangeditor/editor/dist/css/style.css' // 引入 css
import { onBeforeUnmount, shallowRef } from 'vue';
import { Editor, Toolbar } from '@wangeditor/editor-for-vue';
import '@/assets/view.css' //显示文章内容特殊效果

const data = reactive({
  title: null,
  tableData: [],
  pageNum: 1,
  pageSize: 10,
  total: 0,
  formVisible:false,
  form:{},
  ids:[],
  viewVisible:false,
  content:null,
})

/* wangEditor5 富文本初始化开始 */

const baseUrl = 'http://localhost:9090'
const editorRef = shallowRef() // 编辑器实例，必须用 shallowRef
const mode = 'default'
const editorConfig = { MENU_CONF: {} }

// 图片上传配置
editorConfig.MENU_CONF['uploadImage'] = {
  server: baseUrl + '/files/wang/upload', // 服务端图片上传接口
  fieldName: 'file' // 服务端图片上传接口参数
}

// 组件销毁时，也及时销毁编辑器，否则可能会造成内存泄漏
onBeforeUnmount(() => {
  const editor = editorRef.value
  if (editor == null) return
  editor.destroy()
})

// 记录 editor 实例，重要！
const handleCreated = (editor) => {
  editorRef.value = editor
}

/* wangEditor5 初始化结束 */

const view = (content) => {
  data.content = content;
  data.viewVisible = true;
}

// const formRef = ref()  没有表单校验，不用做

const load = () => {
  request.get('/article/selectPage', { //?pageNum=1&pageSize=10
    params: {
      pageNum: data.pageNum,
      pageSize: data.pageSize,
      name:data.name
    }
  }).then(res => {
    data.tableData = res.data.list
    data.total = res.data.total
  })
}
load()

const handleImgSuccess = (res) =>{
  data.form.img = res.data
}

const reset = () => {
  data.name =null
  load()
}

const handleAdd = () => {
  data.formVisible = true;
  data.form = {}//清空脏数据
}

const save = () => { //在一个保存方法里做两个操作，一个新增一个编辑
      data.form.id ? update() : add()
}

const add = () => {
  request.post('/article/add',data.form).then(res =>{ //新增对象无id
    if (res.code === '200'){
      data.formVisible = false
      ElMessage.success( '操作成功')
      load()//新增后重新加载数据
    }else {
      ElMessage.error(res.msg)
    }
  })
}

const handleUpdate = (row) => {
  data.form=JSON.parse(JSON.stringify(row))  //拷贝一个深对象用于编辑，这样就不会影响行对象
  data.formVisible = true
}
const update = () => {
  request.put('/article/update',data.form).then(res =>{ //编辑的对象中包含id
    if (res.code === '200'){
      data.formVisible = false
      ElMessage.success('操作成功')
      load()//更新后重新加载数据
    }else {
      ElMessage.error(res.msg)
    }
  })
}

const del = (id) => {
  ElMessageBox.confirm('确认删除？','删除警告',{type:'warning'}).then(() =>{
    request.delete('article/deleteById/'+id).then(res =>{
      if (res.code === '200'){
        ElMessage.success('操作成功')
        load()//删除后重新加载数据
      }else {
        ElMessage.error(res.msg)
      }
    })
  }).catch()

}

const handleSelectionChange = (rows) => { //返回所有选中的行对象数组
  //从选中的行数组中取出所有的行的id组成一个新的数组
  data.ids = rows.map(row => row.id)
  console.log(data.ids)
}

const delBatch = () => {
  if (data.ids.length ===0 ){
    ElMessage.warning('未选择数据')
    return
  }
  ElMessageBox.confirm('确认删除？','删除警告',{type:'warning'}).then(() =>{
    request.delete('article/deleteBatch' , {data:data.ids}).then(res =>{
      if (res.code === '200'){
        ElMessage.success('操作成功')
        load()//删除后重新加载数据
      }else {
        ElMessage.error(res.msg)
      }
    })
  }).catch()
}


</script>

